<?php
namespace Tbf\Bundle\ImageBundle\Verify;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Tbf\Component\Crypto\Random;
use Tbf\Component\Image\ImageFunction;

class VerifyManager {
    protected $font_path;
    public function __construct($font_path){
        $this->font_path = $font_path;
    }
    public function checkCode($plain,$hash){
        if (hash('sha512',strtoupper($plain))===$hash){
            return true;
        }
        return false;
    }
    /**
     * 生成151x53的一个验证码
     */
    public function g151x53(){
        $verify = Random::imageVerifyString(5);

        //验证码
        $char_width    = imagefontwidth(5);
        $char_height   = imagefontheight(5);

        $string_width  = 15 * $char_width;
        $string_height = 3 * $char_height;

        $img_width	= $string_width  + 16;
        $img_height = $string_height + 8;
        $img = @imagecreatetruecolor($img_width, $img_height) or die("imagecreatetruecolor failed");

        // 颜色
        $background_color = imagecolorallocate($img, 238, 238, 238);
        $bg_text_color	  = imagecolorallocate($img, 191, 191, 191);

        //背景
        imagefill($img ,0, 0, $background_color);

        $bg_char_width  = imagefontwidth(1);
        $bg_char_height = imagefontheight(1);
        for($x = mt_rand(-2, 2); $x < $img_width; $x += $bg_char_width + 1) {
            for($y = mt_rand(-2, 2); $y <  $img_height; $y += $bg_char_height + 1) {
                imagestring($img, mt_rand(1,5), $x,$y, getRandString(1), $bg_text_color);
            }
        }
        $image_rectangle = ImageFunction::newRectangleOnGdImage($img);
        // 验证码
        $x = 0 + mt_rand(-2, 2);
        $y = 0 + mt_rand(-2, 2);
        for($i = 0; $i < strlen($verify); $i++) {
            $text_color		  = imagecolorallocate($img, mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
            //尽量不要出界
            for($j=0;$j<30;$j++){
                $font_size = mt_rand(30,40);
                $angle = mt_rand(-45,45);
                $text = substr($verify,$i,1);
                $font_draw = ImageFunction::newFontDraw()
                    ->setX($x + mt_rand(-2, 2))
                    ->setY($y  + mt_rand(-2, 2))
                    ->setAngle($angle)
                    ->setColorIndex($text_color)
                    ->setSize($font_size)
                    ->setFontfile($this->font_path)
                    ->setText($text);
                if ($font_draw->isInRectangle($image_rectangle)){
                    break;
                }
            }
            $font_draw->draw($img);
            $x += 3 * $char_width;
        }
        ob_start();
        imagepng($img);
        imagedestroy($img);
        $content = ob_get_clean();

        $resp = new VerifyResponse();
        $resp->setPlainCode($verify);
        $resp->setImageContentType('image/png');
        $resp->setImageContent($content);
        return $resp;
    }
    /**
     * 生成72x32的一个简单验证码
     * 4位,纯数值
     * @return VerifyResponse
     */
    public function g72x32Simple(){
        $verify = Random::numbers(4);
        //验证码
        $img_width = 75;
        $img_height = 32;
        $img = @imagecreatetruecolor($img_width, $img_height) or die("imagecreatetruecolor failed");

        // 颜色
        $background_color = imagecolorallocate($img, 255, 255, 255);

        //背景
        imagefill($img ,0, 0, $background_color);

        $x = 0;
        //$y = 32;
        for($i = 0; $i < strlen($verify); $i++) {
            $text_color		  = imagecolorallocate($img, mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
            $y = rand(13,27);
            $hex = rand(-10,10);
            imagettftext($img,16,$hex,$x,$y,$text_color,$this->font_path,substr($verify, $i, 1));
            $x += 75/4;
        }
        ob_start();
        imagepng($img);
        imagedestroy($img);
        $content = ob_get_clean();

        $resp = new VerifyResponse();
        $resp->setPlainCode($verify);
        $resp->setImageContentType('image/png');
        $resp->setImageContent($content);
        return $resp;
    }
}
